package com.sakura.mall.coupon.seckillSession.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.seckillSession.domain.entity.SeckillSessionEntity;
import com.sakura.mall.coupon.seckillSession.domain.request.AddSeckillSessionRequest;
import com.sakura.mall.coupon.seckillSession.domain.request.UpdateSeckillSessionRequest;
import com.sakura.mall.coupon.seckillSession.domain.request.SeckillSessionPageRequest;
import com.sakura.mall.coupon.seckillSession.domain.vo.SeckillSessionVo;
import com.sakura.mall.coupon.seckillSession.mapper.SeckillSessionMapper;
import com.sakura.mall.coupon.seckillSession.service.SeckillSessionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 秒杀活动场次 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionMapper, SeckillSessionEntity> implements SeckillSessionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSeckillSessionRequest request) {
        // todo 其他校验逻辑
        SeckillSessionEntity seckillSession = request.convert(SeckillSessionEntity.class);
        this.save(seckillSession);
    }

    @Override
    public Page<SeckillSessionVo> queryPage(SeckillSessionPageRequest request) {
        IPage<SeckillSessionEntity> qp = request.toPage();
        LambdaQueryWrapper<SeckillSessionEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SeckillSessionVo> ret = qp.convert(entity -> entity.convert(SeckillSessionVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSeckillSessionRequest request) {
        SeckillSessionEntity entity = request.convert(SeckillSessionEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

